const Controller = require('egg').Controller;
const sqlConfig = require('../util/sqlConfig')
const err = require('../util/err')
const moment = require('moment')
const util = require('../util/util')

class BusinessController extends Controller {
    // 推荐列表
    async scAticleTop() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const list = await mysql.query(sqlConfig.scAticleTop(r_body))
        // list.map((item,i)=>{
        //     item.key=i
        //     item.src = item.src.split('&')
        // })

        for(let index = 0; index < list.length; index++){
            const element = list[index]
            const { loginId,src } = element
            element.key=index
            element.src = src.split('&')
            let data2 = await mysql.select('login', {
                where: { loginId }
            })
            if(data2.length){
                element.nickName = data2[0].nickName
                element.avatarUrl = data2[0].avatarUrl
            }
        }
        const total = util.handelData(await mysql.query(sqlConfig.scAticleTopTotal(r_body)))[0]['count(*)']
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {
               list,
               total
            }
        }
    }
    //  获取发现--栏目列表 
    async scAticleType() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { uSchoolId } = r_body
        const mysql = app.mysql
        const data = await mysql.select('scType', {
            where: {
                uSchoolId
            }
        })
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data
        }
    }

    // 发现列表
    async scAticleFind() {
        const { ctx, app } = this;
        const r_body = ctx.request.body;
        const { mysql } = app
        const list = await mysql.query(sqlConfig.scAticleFind(r_body))
        // list.map((item,i)=>{
        //     item.key=i
        //     item.src = item.src.split('&')
        // })
        for(let index = 0; index < list.length; index++){
            const element = list[index]
            const { loginId,src } = element
            element.key=index
            element.src = src.split('&')
            let data2 = await mysql.select('login', {
                where: { loginId }
            })
            if(data2.length){
                element.nickName = data2[0].nickName
                element.avatarUrl = data2[0].avatarUrl
            }
        }
        const total = util.handelData(await mysql.query(sqlConfig.scAticleFindTotal(r_body)))[0]['count(*)']
        ctx.body = {
            code: 200,
            msg: 'SUCCESS',
            data: {
               list,
               total
            }
        }
    }
}



module.exports = BusinessController;